package android.Graphics;

import javax.microedition.khronos.opengles.GL10;
import java.nio.FloatBuffer;

/**
 * Created by IntelliJ IDEA.
 * User: akuryou
 * Date: 4/26/11
 * Time: 4:05 PM
 * To change this template use File | Settings | File Templates.
 */
public class InitTriangle
{
    private float[] triangle;
    private int x;
    private int y;
    private int cellWidth;
    private int cellHeight;
    private int lineWidth;

    private FloatBuffer vertexBuffer;

    public InitTriangle( int x, int y, int cellWidth, int cellHeight, int lineWidth )
    {
        this.x = x;
        this.y = y;
        this.cellWidth = cellWidth;
        this.cellHeight = cellHeight;
        this.lineWidth = lineWidth;
        initTriangle();
    }

    public void initTriangle()
    {
        triangle = triangleArray( x, y, cellWidth, cellHeight, lineWidth );
        vertexBuffer = FloatBuffer.allocate( triangle.length );
        vertexBuffer = FloatBuffer.wrap( triangle );
        vertexBuffer.put( triangle );
        vertexBuffer.position( 0 );
    }

    public float[] triangleArray( int x, int y, int cellWidth, int cellHeight, int lineWidth )
    {
        triangle = new float[6];

        //Top
        triangle[0] = x * cellWidth + cellWidth/2;
        triangle[1] = (y+1) * cellHeight - 3*lineWidth;
        //Bottom left
        triangle[2] = x * cellWidth + 3*lineWidth;
        triangle[3] = y * cellHeight + 3*lineWidth;
        //Bottom right
        triangle[4] = (x+1) * cellWidth - 3*lineWidth;
        triangle[5] = triangle[3];

        return triangle;
    }

    public void drawTriangle( GL10 gl )
    {
        gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertexBuffer );
        gl.glEnableClientState( GL10.GL_VERTEX_ARRAY );
        gl.glDrawArrays( GL10.GL_LINE_LOOP, 0, triangle.length/2 );
    }
}
